.text
    .align 4
    .global begin
    .type begin, function
    
.set num,20                 @ Set number of words to be copied

begin:
    ldr     r0, =src        @ r0 = pointer to source block
    ldr     r1, =dst        @ r1 = pointer to destination block
    mov     r2, #num        @ r2 = number of words to copy
    ldr     sp, =stack      @ set up stack pointer (r13)

blockcopy:
    movs    r3,r2, lsr #3   @ number of eight word multiples
    beq     copywords       @ less than eight words to move ?
    stmfd   sp!, {r4-r11}   @ save some working registers

octcopy:
    ldmia   r0!, {r4-r11}   @ load 8 words from the source
    stmia   r1!, {r4-r11}   @ and put them at the destination
    subs    r3, r3, #1      @ decrement the counter
    bne     octcopy         @ ... copy more
    ldmfd   sp!, {r4-r11}   @ don't need these now - restore originals

copywords:
    ands    r2, r2, #7      @ number of odd words to copy
    beq     stop            @ No words left to copy ?

wordcopy:
    ldr     r3, [r0], #4    @ a word from the source
    str     r3, [r1], #4    @ store a word to the destination
    subs    r2, r2, #1      @ decrement the counter
    bne     wordcopy        @ ... copy more

stop:
    nop
    b stop

.data
src:
    .word 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4
dst:
    .word 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
stack:
    .space 4096
